package org.rabbitmq.mq02.mqServer.datacore;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.rabbitmq.mq02.Mq02Application;
import org.rabbitmq.mq02.mqServer.core.Binding;
import org.rabbitmq.mq02.mqServer.core.Exchange;
import org.rabbitmq.mq02.mqServer.core.ExchangeType;
import org.rabbitmq.mq02.mqServer.core.MSGQueue;
import org.rabbitmq.mq02.mqServer.mapper.MetaMapper;

import java.io.File;
import java.util.List;

/**
 * 通过这个类来封装针对数据库的操作.
 */
public class DataBaseManager {
//通过手动管理,非spring注入
    private MetaMapper meatMapper;

    public void init() {
        //手动管理数据
        meatMapper = Mq02Application.context.getBean(MetaMapper.class);
        //建库建表
        //1.先查询数据库是否已经存在
        if(!checkDBExists()){
            //不存在时,先建库建表
            File file = new File("./data");
            //先创建目录
            file.mkdirs();
            //创建表
            createTable();
            //初始化,主要初始化 交换机的类型
            createDefaultExchange();
            System.out.println("[DataBaseManager] 数据库初始化完成!");
        }else{
            System.out.println("[DataBaseManager] 数据库以存在!");
        }
    }

    //删除数据库及创建的目录:
    public void deleteTable() {
        File file = new File("./data/meta.db");
        //1.删除数据库:
        boolean ok = file.delete();
        if(ok){
            System.out.println("[DataBaseManager] 删除数据库成功!");
        }else{
            System.out.println("[DataBaseManager] 删除数据库失败!");
        }
        //2.删除目录:
        File file1 = new File("./data");
        ok = file1.delete();
        if(ok){
            System.out.println("[DataBaseManager] 删除目录成功!");
        }else{
            System.out.println("[DataBaseManager] 删除目录失败!");
        }
    }

//初始化交换机
    private void createDefaultExchange() {
        Exchange exchange = new Exchange();
        exchange.setType(ExchangeType.DIRECT);
        exchange.setName("");
        exchange.setAutoDelete(false);
        exchange.setDurable(false);
        meatMapper.insertExchange(exchange);
        System.out.println("[DataBaseManager] 初始化交换机成功!");
    }

    private void createTable() {
        meatMapper.createExchangeTable();
        meatMapper.createQueueTable();
        meatMapper.createBindingTable();
        System.out.println("[DataBaseManager] 建表成功!");
    }

    //查询数据库是否已经创建
    private boolean checkDBExists() {
        File file = new File("./data/meta.db");
        return file.exists();
    }

//封装其他数据库操作:对交换机 队列 绑定关系的 增删查
    public void insertExchange(Exchange exchange){
    meatMapper.insertExchange(exchange);
}
    public List<Exchange> selectAllExchanges(){
        return meatMapper.selectAllExchanges();
    }
    public void deleteExchange(String exchangeName){
        meatMapper.deleteExchange(exchangeName);
    }

    public void insertQueue(MSGQueue queue){
        meatMapper.insertQueue(queue);
    }
    public List<MSGQueue> selectAllQueues(){
        return meatMapper.selectAllQueues();
    }
    public void deleteQueue(String queueName){
        meatMapper.deleteQueue(queueName);
    }

    public void insertBinding(Binding binding){
        meatMapper.insertBinding(binding);
    }
    public List<Binding> selectAllBindings(){
        return meatMapper.selectAllBindings();
    }
    public void deleteBindings(String bindingKey){
        meatMapper.deleteBinding(bindingKey);
    }


}
